import Vue from 'vue';
import Router from 'vue-router';
import {isLogin} from '@/auth/authService';

/* eslint-disable */
const login = r => require.ensure([], () => r(require('../pages/login')), 'login');
const index = r => require.ensure([], () => r(require('../pages/index')), 'index');
const discover = r => require.ensure([], () => r(require('../pages/discover')), 'discover');
const publish = r => require.ensure([], () => r(require('../pages/publish')), 'publish');
const userinfo = r => require.ensure([], () => r(require('../pages/userinfo')), 'userinfo');
const home = r => require.ensure([], () => r(require('../pages/home')), 'home');
const addProcess = r => require.ensure([], () => r(require('../pages/add/process')), 'addProcess');
const addArticle = r => require.ensure([], () => r(require('../pages/add/article')), 'addArticle');
const reviewProcess = r => require.ensure([], () => r(require('../pages/review/process')), 'reviewProcess');
const reviewArticle = r => require.ensure([], () => r(require('../pages/review/article')), 'reviewArticle');


Vue.use(Router);

const router = new Router({
    routes: [
        {
            path: '/',
            name: 'index',
            component: index,
        },
        {
            path: '/login',
            name: 'login',
            component: login,
        },
        {
            path: '/discover',
            name: 'discover',
            component: discover,
        },
        {
            path: '/publish',
            name: 'publish',
            component: publish,
        },
        {
            path: '/userinfo',
            name: 'userinfo',
            component: userinfo,
            meta:{
                requiresUserInfo: true
            }
        },
        {
            path: '/home',
            name: 'home',
            component: home,
            meta:{
                requiresAuth: true
            }
        },
        {
            path: '/addProcess',
            name: 'addProcess',
            component: addProcess,
            meta:{
                requiresAuth: true
            }
        },
        {
            path: '/addArticle',
            name: 'addArticle',
            component: addArticle,
            meta:{
                requiresAuth: true
            }
        },
        {
            path: '/reviewArticle',
            name: 'reviewArticle',
            component: reviewArticle
        },
        {
            path: '/reviewProcess',
            name: 'reviewProcess',
            component: reviewProcess
        }
    ]
});

router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.goTop)) {
        window.scroll(0, 0);
    }
    if(to.matched.some(record => record.meta.requiresAuth)) {
        if(!isLogin()) {
            return next({path: '/login?type=login'});
        }
    }
    next();
});

export default router;
